package com.sie.framework.device;

import android.bluetooth.BluetoothDevice;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;

import com.bankcomm.peripheral.EmvOptions;
import com.sie.framework.appinterface.Api;
import com.sie.framework.bean.BankCardBean;
import com.sie.framework.bean.IdentityBean;
import com.sie.framework.constconfig.ErrorDefine;
import com.sie.framework.channel.ChannelInstance;
import com.sie.framework.listen.CardReadListen;
import com.sie.framework.listen.FingerPrintListen;
import com.sie.framework.listen.KeyBroadListen;
import com.sie.framework.listen.LogCallback;
import com.sie.framework.module.BankCardModule;
import com.sie.framework.module.FingerPrintModule;
import com.sie.framework.listen.IdentityListen;
import com.sie.framework.module.IdentityModule;
import com.sie.framework.module.KeyBroadModule;
import com.sie.framework.module.KeyBroadOption;
import com.sie.tools.ByteTool;
import com.sie.tools.LogUtil;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by zhq on 2015/12/10.
 */
public class SiecomTaskQueue {
	// protected static Handler handler = null;
	private static final String TAG = SiecomTaskQueue.class.getSimpleName();
	protected static ArrayBlockingQueue<SiecomTask> taskQueue = new ArrayBlockingQueue<SiecomTask>(
			1);
	private TaskDispatcher mTaskDispatcher;
	private static SiecomTaskQueue instance = null;
	private SiecomTask mTask;
	private final static String org = "ZG";
	private static ExecutorService threadPool = Executors.newFixedThreadPool(1);
	Handler handler;
	public FingerPrintModule module;
	public static enum Result {
		START, SUCCEED, ERROR
	}

	private SiecomTaskQueue() {
		instance = this;
		taskQueue.clear();
	//	handler = new Handler(Looper.getMainLooper());
		mTaskDispatcher = new TaskDispatcher(taskQueue, this, null);
		mTaskDispatcher.start();

	}

	public static SiecomTaskQueue getInstance() {
		if (instance == null) {
			instance = new SiecomTaskQueue();
		}
		return instance;
	}

	public void cancelCurrentTask(boolean isConnected) {
		if (mTaskDispatcher.getCurrentTask() != null) {
			mTaskDispatcher.getCurrentTask().setCancel(true, isConnected);
			
			if(mTaskDispatcher.getCurrentTask().operate==SiecomTask.Operate.FINGER_PRINT){
				
				mTaskDispatcher.getCurrentTask().taskCallback =null;
			}
//			if(mTask.getOperate() == SiecomTask.Operate.FINGER_PRINT 
//					|| mTask.getOperate() == SiecomTask.Operate.READ_IDENTITY
//					|| mTask.getOperate() == SiecomTask.Operate.GET_PIN
//					|| mTask.getOperate() == SiecomTask.Operate.READ_IC_CARD
//					|| mTask.getOperate() == SiecomTask.Operate.READ_PIC_CARD
//					|| mTask.getOperate() == SiecomTask.Operate.READ_MSR_CARD
//					|| mTask.getOperate() == SiecomTask.Operate.READ_AUTO_FIND){
//				
//				Bundle bundle = new Bundle();
//				bundle.putInt("code", -11);
//				bundle.putString("message", "已经取消操作");
//
//				deliverResult(mTask, bundle, Result.ERROR);
//				
//				
//			}
			
		}
	}

	public void jiaotong_CancelTask(boolean isConnected){
		
		
		if(isConnected == true){
			
			if(mTask.getOperate() == SiecomTask.Operate.FINGER_PRINT){
				FingerPrintModule.getInstance().closeModule(true);
			}else if(mTask.getOperate() == SiecomTask.Operate.READ_IDENTITY){
				IdentityModule.getInstance().closeModule(true);
			}else if(mTask.getOperate() == SiecomTask.Operate.GET_PIN){
				KeyBroadModule.getInstance().closeModule(true);
			}else{
				BankCardModule.getInstance().closeModule(true);
//				mTaskDispatcher.getCurrentTask().setCancel(true, isConnected);
			}
			
			Bundle bundle = new Bundle();
			bundle.putString("code", "-11");
			bundle.putString("message", "已经取消操作");

//			SiecomTask.deliverError(bundle);
			deliverResult(mTask, bundle, Result.ERROR);
		}else{
			if(mTask.getOperate() == SiecomTask.Operate.FINGER_PRINT){
				FingerPrintModule.getInstance().closeModule(false);
			}else if(mTask.getOperate() == SiecomTask.Operate.READ_IDENTITY){
				IdentityModule.getInstance().closeModule(false);
			}else if(mTask.getOperate() == SiecomTask.Operate.GET_PIN){
				KeyBroadModule.getInstance().closeModule(false);
			}else{
				BankCardModule.getInstance().closeModule(false);
//				mTaskDispatcher.getCurrentTask().setCancel(true, isConnected);
			}
			
			Bundle bundle = new Bundle();
			bundle.putString("code", ErrorDefine.ERR_DEVICE_DISCONNECTED);
			bundle.putString("message", "设备连接断开");

//			SiecomTask.deliverError(bundle);
			deliverResult(mTask, bundle, Result.ERROR);
			
		}

		if (mTaskDispatcher != null && mTaskDispatcher.getCurrentTask() != null) {
			
			if(mTaskDispatcher.getCurrentTask().operate==SiecomTask.Operate.FINGER_PRINT){
				
				mTaskDispatcher.getCurrentTask().taskCallback =null;
			}

		}

		
	}
	public void addTask(SiecomTask task) {
		//Log.e("GJ", "taskadd"+task.getOperate());
		mTask = task;
		// 清除正在运行的任务

		SiecomTask lastTask = mTaskDispatcher.getCurrentTask();
		if (lastTask != null) {
			if (lastTask.operate == task.operate) {
				// 如果上一次任务没有完成且没有被取消
				if(lastTask.operate==SiecomTask.Operate.FINGER_PRINT||
						lastTask.operate==SiecomTask.Operate.READ_IC_CARD||
						lastTask.operate==SiecomTask.Operate.READ_PIC_CARD){
					
//					cancelCurrentTask(true);
					LogUtil.e(TAG,"lastTaskOperate == " + lastTask.operate);
					
				}else if (!lastTask.isCancel()&&!lastTask.isFinish()) {
					Log.e("repeate","=====!!!!!!");
					Bundle bundle = new Bundle();
					bundle.putString("code", "-879");
					bundle.putString("message", "您已经执行同样的操作且操作还在执行，请稍等");
					deliverResult(task, bundle, Result.ERROR);
					return;
				}
			}
//			cancelCurrentTask(true);
		}
		try {
			taskQueue.add(task);
		} catch (IllegalStateException e) {
			e.printStackTrace();
			cancelCurrentTask(true);

		}
	}

	/**
	 * 处理任务
	 * 
	 * @param task
	 */
	protected void performanceTask(final SiecomTask task) {
		LogUtil.e(TAG, "performanceTask : " + task.operate.name());
		switch (task.operate) {
		case CONNECT_BT:
			connectBt(task);
			break;
		case READ_IDENTITY:
			ReadIdentity(task);
			break;
		case FINGER_PRINT:
			ReadFinger(task);
			break;
		case INPUT_MAIN_KEY:
			KeyBorad(task, KeyBroadModule.OPCODE_MAIN_KEY);
			break;
		case INPUT_WORK_KEY:
			KeyBorad(task, KeyBroadModule.OPCODE_WORK_KEY);
			break;
		case GET_PIN:
			KeyBorad(task, KeyBroadModule.OPCODE_GET_PIN);
			break;

		case READ_AUTO_FIND:
			ReadBankCard(task, BankCardModule.AUTO_FIND);
			break;
		case READ_IC_CARD:
			ReadBankCard(task, BankCardModule.IC_CARD);
			break;
		case READ_PIC_CARD:
			ReadBankCard(task, BankCardModule.PIC_CARD);
			break;
		case READ_MSR_CARD:
			ReadBankCard(task, BankCardModule.MSR_CARD);
			break;
		case GET_PUK:
			getPuk(task);
			break;

		case RSA_DECIPHER:
			rsaDecipher(task);
			break;
		case GET_SN:
			getSerialNo(task);
			break;
		  case READ_LOG:
              ReadCardLog(task,0);
              break;
          case READ_LOAD_LOG:
              ReadCardLog(task,1);
              break;
          case READ_INITINFO:
        	  getInitInfo(task);
              break;
          case READ_LOADMAINKEYCN:
        	  loadMainKeyCN(task);
              break;
              
		}

	}
	 private void ReadCardLog(final SiecomTask task,int logType) {
	        BankCardModule module = BankCardModule.getInstance();
	        task.setModule(module);
	        LogCallback listen = new LogCallback() {

	            @Override
	            public void onStart() {
	                Bundle bundle = new Bundle();
	                deliverResult(task, bundle, Result.START);
	            }

	            @Override
	            public void onReadLog(byte[] buff) {
	                // TODO Auto-generated method stub

	                Bundle bundle = new Bundle();
	                bundle.putByteArray("data",buff);
	                deliverResult(task, bundle, Result.SUCCEED);
	            }

	            @Override
	            public void onReadFail(String ret, String msg) {
	                // TODO Auto-generated method stub
	                Bundle bundle = new Bundle();
	                bundle.putString("code", ret);
	                bundle.putString("message", msg);
	                deliverResult(task, bundle, Result.ERROR);

	            }
	        };
	        task.startCountDown();
	        module.startReadLog((Integer) task.getParams(),logType,listen);

	    }
	 
	 
		private void loadMainKeyCN(final SiecomTask task) {
			// TODO Auto-generated method stub
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					KeyBroadOption mBroad = (KeyBroadOption)task.getParams();
					
//					//Log.e("GJ", mBroad.mixMk);
					byte[] mixMk = ByteTool.hexStr2Bytes(mBroad.mixMk);
					byte[] bankSignMixMK = ByteTool.hexStr2Bytes(mBroad.bankSignMixMK);
					byte[] pkBank = ByteTool.hexStr2Bytes(mBroad.pkBank);
					byte[] vndSignBank = ByteTool.hexStr2Bytes(mBroad.vndSignBank);
					Api api = new Api();
					byte[] checkData = new byte[16];
					int ret = -1;
					boolean flag = false;
					for(int i=0;i<10;i++){
						ret = api.EmvLib_loadMainKeyCN(mixMk,bankSignMixMK,pkBank,vndSignBank,mBroad.mianKeyCode,checkData,mBroad.cipherTpye);
						if(!checkByteAllZero(checkData)){
							flag = true;
							break;
						}
						SystemClock.sleep(100);
					}
////					//Log.e("GJ", "数据等于"+ByteTool.byte2hex(code));
					if(!flag){
						Bundle bundle = new Bundle();
						bundle.putString("code", ErrorDefine.ERR_GET_SN_FAIL+"");
						bundle.putString("message", "写入主密钥失败");
						deliverResult(task, bundle, Result.ERROR);
						return;
					}
					if (ret == 0) {
						Bundle bundle = new Bundle();
						bundle.putByteArray("data", checkData);
						deliverResult(task, bundle, Result.SUCCEED);
					} else {
						Bundle bundle = new Bundle();
						bundle.putString("code", ErrorDefine.ERR_GET_SN_FAIL+"");
						bundle.putString("message", "写入主密钥失败");
						deliverResult(task, bundle, Result.ERROR);
					}

				}
			}).start();

		}
		
		private void getInitInfo(final SiecomTask task) {
			// TODO Auto-generated method stub
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					Api api = new Api();
					byte[] code = new byte[144];
					int ret = -1;
					boolean flag = false;
//					for(int i=0;i<1;i++){
						ret = api.SysSysUniqId(code,(byte)1,(byte)1,(byte)0x00);
						if(!checkByteAllZero(code)){
							flag = true;
//							break;
						}
//						SystemClock.sleep(100);
//					}
//					//Log.e("GJ", "数据等于"+ByteTool.byte2hex(code));
					if(ret == -1){
						Bundle bundle = new Bundle();
						bundle.putString("code", ErrorDefine.ERR_GET_SN_FAIL+"");
						bundle.putString("message", "没有该功能需要升级固件");
						deliverResult(task, bundle, Result.ERROR);
						return;
					}
					if(!flag){
						Bundle bundle = new Bundle();
						bundle.putString("code", ErrorDefine.ERR_GET_SN_FAIL+"");
						bundle.putString("message", "获取序列号失败");
						deliverResult(task, bundle, Result.ERROR);
						return;
					}
					if (ret == 0) {
						Bundle bundle = new Bundle();
						bundle.putByteArray("data", code);
						deliverResult(task, bundle, Result.SUCCEED);
					} else {
						Bundle bundle = new Bundle();
						bundle.putString("code", ErrorDefine.ERR_GET_SN_FAIL+"");
						bundle.putString("message", "获取序列号失败");
						deliverResult(task, bundle, Result.ERROR);
					}

				}
			}).start();

		}
		
	private void getSerialNo(final SiecomTask task) {
		// TODO Auto-generated method stub
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Api api = new Api();
				byte[] code = new byte[16];
				int ret = -1;
				boolean flag = false;
				for(int i=0;i<2;i++){
					ret =api.SysUniqId(code);
					//Log.e("GJjiaohang", "获取序列号返回值"+ret);
					if(!checkByteAllZero(code)){
						flag = true;
						break;
					}
					SystemClock.sleep(100);
				}
				if(!flag){
					Bundle bundle = new Bundle();
					bundle.putString("code", ErrorDefine.ERR_GET_SN_FAIL+"");
					bundle.putString("message", "获取序列号失败");
					deliverResult(task, bundle, Result.ERROR);
					return;
				}
				if (ret == 0) {
					Bundle bundle = new Bundle();
					bundle.putByteArray("data", code);
					deliverResult(task, bundle, Result.SUCCEED);
				} else {
					Bundle bundle = new Bundle();
					bundle.putString("code", ErrorDefine.ERR_GET_SN_FAIL+"");
					bundle.putString("message", "获取序列号失败");
					deliverResult(task, bundle, Result.ERROR);
				}

			}
		}).start();

	}
	public boolean checkByteAllZero(byte[] code ){
	
		for(byte b:code){
			if(b!=(byte)0x00){
				
				return false;
			}
		}
		return true;
		
	}

	private void rsaDecipher(final SiecomTask task) {
		// TODO Auto-generated method stub
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				PubOp op = (PubOp) task.getParams();
				Api api = new Api();
				byte slot = 0x01;
				byte vlot = 0x01;
				byte pubKeyIndex = 0x00;
				if (op.cipherTpye == 1) {
					
					byte[] checkData = new byte[16];
					int[] len = new int[1];
					int ret = api.RsaDecipher(slot, vlot, pubKeyIndex,
							(byte) op.code, op.content, checkData, len,
							task.getTimeOut());
					if (ret == 0) {
						byte[] checkTmp = new byte[8];
						System.arraycopy(checkData, 0, checkTmp, 0, 8);
						Bundle bundle = new Bundle();
						bundle.putByteArray("data", checkTmp);
						deliverResult(task, bundle, Result.SUCCEED);
					} else {
						Bundle bundle = new Bundle();
						bundle.putString("code", ErrorDefine.ERR_LOAD_MAIN_KEY_FAIL+"");
						bundle.putString("message", "主密钥下载异常");
						deliverResult(task, bundle, Result.ERROR);
					}
				}else if(op.cipherTpye==2){
					
					op.code = (byte) (op.code+10);
					
					byte[] checkData = new byte[16];
					int[] len = new int[1];
					int ret = api.SM2Decipher(slot, vlot, pubKeyIndex,
							(byte) op.code, op.content, checkData, len,
							task.getTimeOut());
					if (ret == 0) {
						byte[] checkTmp = new byte[8];
						System.arraycopy(checkData, 0, checkTmp, 0, 8);
						Bundle bundle = new Bundle();
						bundle.putByteArray("data", checkTmp);
						deliverResult(task, bundle, Result.SUCCEED);
					} else {
						Bundle bundle = new Bundle();
						bundle.putString("code", ErrorDefine.ERR_LOAD_MAIN_KEY_FAIL+"");
						bundle.putString("message", "下载主密钥失败");
						deliverResult(task, bundle, Result.ERROR);
					}
					
				}

			}
		}).start();

	}

	/**
	 * 1是RSA，2是SM2
	 * 
	 * @param task
	 */
	private void getPuk(final SiecomTask task) {

		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Api api = new Api();
				int cipherTpye = (int) task.getParams();
				if (cipherTpye == 1) {
					byte slot = 0x01;
					byte vlot = 0x01;
					byte pubKeyIndex = 0x00;
					short puklen = 256;
					byte[] pubKeyData = new byte[256];
					int[] len = new int[1];
					int ret = api.getPubRsaPuk(slot, vlot, pubKeyIndex, puklen,
							pubKeyData, len, task.getTimeOut());
					if (ret == 0) {
						Bundle bundle = new Bundle();
						bundle.putByteArray("data", pubKeyData);
						deliverResult(task, bundle, Result.SUCCEED);
					} else {
						Bundle bundle = new Bundle();
						bundle.putString("code", ErrorDefine.ERR_GET_PUBLIC_KEY_FAIL+"");
						bundle.putString("message", "获取公钥失败");
						deliverResult(task, bundle, Result.ERROR);
					}
				} else if (cipherTpye == 2) {

					int ret = api.NewSM2KeyPair(1, 1, 0);
					Log.e("ret:", ret + "====");
					byte[] sm2puk = new byte[64];
					ret = api.UpdateSM2Key((byte) 0x01, (byte) 0x01,
							(byte) 0x00, 0, new byte[0], sm2puk);
					Log.e("ret:", ret + "====");
					Log.e("puk:", ByteTool.byte2hex(sm2puk));
					if (ret == 0) {
						Bundle bundle = new Bundle();
						bundle.putByteArray("data", sm2puk);
						deliverResult(task, bundle, Result.SUCCEED);
					} else {
						Bundle bundle = new Bundle();
						bundle.putString("code", ErrorDefine.ERR_GET_PUBLIC_KEY_FAIL+"");
						bundle.putString("message", "获取公钥失败");
						deliverResult(task, bundle, Result.ERROR);
					}

				}
			}
		}).start();

	}

	private void ReadBankCard(final SiecomTask task, int cardType) {
		BankCardModule module = BankCardModule.getInstance();
		task.setModule(module);
		CardReadListen listen = new CardReadListen() {

			@Override
			public void onStart() {
				Bundle bundle = new Bundle();
				deliverResult(task, bundle, Result.START);
			}

			@Override
			public void onReadCardInfo(BankCardBean bean) {
				// TODO Auto-generated method stub
				// Log.e("card", bean.cardNo);
				Bundle bundle = new Bundle();
				bundle.putParcelable("data", bean);
				deliverResult(task, bundle, Result.SUCCEED);
			}

			@Override
			public void onReadCardFail(String ret, String msg) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putString("code", ret + "");
				bundle.putString("message", msg);
				deliverResult(task, bundle, Result.ERROR);

			}
		};
		task.startCountDown();
		module.setEMVOption((EmvOptions) task.getParams());
		module.setCallback(listen);
		module.startReadCard(cardType);

	}

	private void KeyBorad(final SiecomTask task, int opCode) {

		KeyBroadModule module = KeyBroadModule.getInstance();
		task.setModule(module);
//		task.startCountDown();
		KeyBroadListen listen = new KeyBroadListen() {

			@Override
			public void onStart() {
				Bundle bundle = new Bundle();
				deliverResult(task, bundle, Result.START);
			}

			@Override
			public void onSucc(byte[] checkdata) {
				// TODO Auto-generated method stub
				byte[] checkTmp = new byte[8];
				System.arraycopy(checkdata, 0, checkTmp, 0, 8);
				Bundle bundle = new Bundle();
				bundle.putByteArray("data", checkTmp);
				Log.e("checkdata", ByteTool.byte2hex(checkdata));
				deliverResult(task, bundle, Result.SUCCEED);
			}

			@Override
			public void onReadPin(byte[] pin) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putInt("option", KeyBroadModule.OP_FINISH);
				bundle.putByteArray("password", pin);
				deliverResult(task, bundle, Result.SUCCEED);
			}

			@Override
			public void onFail(String ret, String msg) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putString("code", ret + "");
				bundle.putString("message", msg);
				deliverResult(task, bundle, Result.ERROR);
			}

			@Override
			public void onCancel() {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putInt("option", KeyBroadModule.OP_CANCEL);
				deliverResult(task, bundle, Result.SUCCEED);
			}

			@Override
			public void onKeyNum(int num) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putInt("option", KeyBroadModule.OP_INPUTTING);
				bundle.putInt("keyNum", num);
				deliverResult(task, bundle, Result.SUCCEED);

			}

		};
		
		module.startRun(listen, opCode, (KeyBroadOption) task.getParams());

	}

	private void ReadFinger(final SiecomTask task) {

		task.startCountDown();
		module = FingerPrintModule.getInstance();
		task.setModule(module);// 自动关闭
		
		FingerPrintListen callback = new FingerPrintListen() {
			@Override
			public void onStart() {
				Bundle bundle = new Bundle();
				deliverResult(task, bundle, Result.START);
			}

			@Override
			public void onRead(String code) {
				Bundle bundle = new Bundle();
				bundle.putString("data", code);
				deliverResult(task, bundle, Result.SUCCEED);
			}

			@Override
			public void onFail(String ret, String msg) {

				Bundle bundle = new Bundle();
				bundle.putString("code", ret + "");
				bundle.putString("message", msg);
				deliverResult(task, bundle, Result.ERROR);

			}

			@Override
			public void onGetImgSucc(Bitmap fingerPrintBMP) {

			}

			@Override
			public void onGetImgProgress(int Progress) {

			}
		};
		module.setCallback(callback);
		module.startRead((int) task.getParams());

	}

	private void ReadIdentity(final SiecomTask task) {
		// 超时统计

		task.startCountDown();
		IdentityModule module = IdentityModule.getInstance();
		task.setModule(module);// 自动关闭

		IdentityListen callback = new IdentityListen() {
			@Override
			public void onStart() {
				Bundle bundle = new Bundle();
				deliverResult(task, bundle, Result.START);
			}

			@Override
			public void onReadSucc(IdentityBean bean) {
				Bundle bundle = new Bundle();
				bundle.putParcelable("data", bean);
				deliverResult(task, bundle, Result.SUCCEED);

			}

			@Override
			public void onReadFail(String ret, String msg) {
				LogUtil.mLog("IDCard  ret:"+ret+"--msg:"+msg);
				Bundle bundle = new Bundle();
				bundle.putString("code", ret + "");
				bundle.putString("message", msg);
				deliverResult(task, bundle, Result.ERROR);
			}
		};
		module.setCallback(callback);
		module.startRead((boolean) task.getParams());
	}

	private void connectBt(final SiecomTask task) {
		Runnable r = new Runnable() {
			@Override
			public void run() {
				Bundle bundle1 = new Bundle();
				deliverResult(task, bundle1, Result.START);
				int res = ChannelInstance.initDevice(ChannelInstance.BTCONNECT,
						(BluetoothDevice) task.getParams());
				Log.e("connectBt", res + "!!");
				if (res == 0) {
					Bundle bundle = new Bundle();
					bundle.putParcelable("data",
							(BluetoothDevice) task.getParams());
					deliverResult(task, bundle, Result.SUCCEED);
				} else {
					Bundle bundle = new Bundle();
					bundle.putInt("code", res);
					bundle.putString("message",
							ErrorDefine.getErrorDescribe(res));
					deliverResult(task, bundle, Result.ERROR);
				}
			}
		};
		threadPool.submit(r);
	}

	protected void deliverResult(final SiecomTask task, final Bundle bundle,
			final Result result) {
		

		switch (result) {
		case START:
			task.deliverStart(bundle);
			break;
		case SUCCEED:
			task.deliverResponse(bundle);
			break;
		case ERROR:
			task.deliverError(bundle);
			break;
		}
		// if (null != handler) {
		// if (null != task && !task.isCancel()) {
		// handler.post(new Runnable() {
		// @Override
		// public void run() {
		// switch (result) {
		// case START:
		// task.deliverStart(bundle);
		// break;
		// case SUCCEED:
		// task.deliverResponse(bundle);
		// break;
		// case ERROR:
		// task.deliverError(bundle);
		// break;
		// }
		// }
		// });
		// }
		// }
	}

	public void cancelTaskDispatcher() {
		if (null != mTaskDispatcher) {
			mTaskDispatcher.cancle();

		}
	}
}
